var api = 'https://apiv2.httoutiao.com/';  //  'http://192.168.9.160/'; //
var url = this.GetRequest();
var platform = url.platform;
var token = url.token;
new Vue({
	el:"#app",
	data:{
	  tian:[
		  {num:""},
		  {num:""},
	  ],
	  shi:[
		{num:""},
		{num:""},
	  ],
	  fen:[
		{num:""},
		{num:""},
	  ],
	  toast:false,  
	  toastMsg:'错误信息',    
	  zhuTrue:true,
	  roleTrue:false, 
	  yaoqing:false,
	  yaoqingImg:'',
	  now_time:'',
	  end_time:'',
	  invite_count:0,
	  index:[],
	  notice:[],
	  animate:false,
	  daiTrue:false,
	  daiQ:0,
	  daiR:1,
	  dengdai:0,
	  //timer1:'',
	},
	
	mounted(){
		let that = this;
		

		var url= api +"user/invite/index";
        var a = that.qingqiu('',platform,token);
        jQuery.post(url,a,function(d){
           if(d.status == 0){
				var res = d.data;
				that.index = res;
				that.now_time = res.now_time;
				that.end_time = res.end_time;
				that.invite_count = res.invite_count;

				that.fresh()
				setInterval(that.fresh(),100);
				$(".loading").hide();

				

		   }else{
				if(d.status == 5 || d.status == 6){
					if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {

						window.webkit.messageHandlers.login.postMessage('');
		
					} else if (navigator.userAgent.match(/android/i)) {
		
						window.AndroidNative.login();
		
					}
					
				}else{
					that.toastShow(d.msg);
				}
		   }
		})
		
		var url= api +"user/invite/phone";
        var a = that.qingqiu('',platform,token);
        jQuery.post(url,a,function(d){
           if(d.status == 0){
				var res = d.data;
				that.notice = res;
				that.timer1= setInterval(that.scroll,3000)
		   }else{
				that.toastShow(d.msg);
		   }
		})
		
		
		   
	},
	
	methods:{
		fanhui(){
			if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {

				window.webkit.messageHandlers.returnToApp.postMessage('');

			} else if (navigator.userAgent.match(/android/i)) {

				window.AndroidNative.returnToApp();

			}
		},
		fanhui1(){
			this.roleTrue = false;
			this.yaoqing = false;
			this.daiTrue = false;
			this.zhuTrue= true;
		},
		guize(){
			// this.roleTrue = true;
			// this.zhuTrue= false;
			window.location.href="rule.html?token="+token+"platform="+platform
		},
		scroll(){
			let con1 = this.$refs.con1;
			
			con1.style.marginTop="-30px";
			this.animate=!this.animate;
			var that = this; // 在异步函数中会出现this的偏移问题，此处一定要先保存好this的指向
			setTimeout(function(){
				//that.notice.push(that.notice[0]); 
				//console.log(that.notice)
				that.notice.shift();
				//console.log(that.notice.length)
				con1.style.marginTop='0px';
				that.animate=!that.animate; // 这个地方如果不把animate 取反会出现消息回滚的现象，此时把ul 元素的过渡属性取消掉就可以完美实现无缝滚动的效果了

				if(that.notice.length == 3){
					var url= api +"user/invite/phone";
					var a = that.qingqiu('',platform,token);
					jQuery.post(url,a,function(d){
						if(d.status == 0){
								var res = d.data;
								for(var i=0;i<res.length;i++){
									that.notice.push(res[i]);
								}
						}else{
								that.toastShow(d.msg);
						}
					})
				}

			},500)
		},
		mEnter () {
			
			clearInterval(this.timer1)
			
		},
		mLeave () {
			
			this.timer1= setInterval(this.scroll,3000)
			
			
		},
		yaoqing1(){
			let that = this;
			var url= api +"user/prize/index";
			
			var a = that.qingqiu('',platform,token);

			console.log(that.dengdai)
			if(that.dengdai == 0){
				that.dengdai = 1;
				jQuery.post(url,a,function(d){
					
					if(d.status == 0){
						for(var i=0;i<d.data.length;i++){
							if(d.data[i].type == 1){
								that.yaoqingImg = d.data[0].share_img;
								that.zhuTrue = false;
								that.daiTrue = false;
								that.yaoqing = true;
							}
						}
						
						
					}else{
						that.toastShow(d.msg)
						that.dengdai = 0;
					}
				})
			}
			
			
		},
		lingqu(i){
			
			let that = this;
			var url= api +"user/invite/bonus";
			var d="[{'money':'"+i+"'}]"
			d = eval('(' + d + ')');
			
			var a = that.qingqiu(d,platform,token);

			console.log(that.index.packet_one_status)

			if(i == 1 && that.index.packet_one_status == 1){
				console.log('可领取1元')
				
				jQuery.post(url,a,function(d){
					if(d.status == 0){
						that.index.packet_one_status = 2
					}
					that.toastShow(d.msg)
				})
			}else if(i == 2 && that.index.packet_two_status == 1){
				console.log('可领取2元')
				jQuery.post(url,a,function(d){
					if(d.status == 0){
						that.index.packet_two_status = 2
					}
					that.toastShow(d.msg)
				})
			}else if(i == 7 && that.index.packet_five_status == 1){
				console.log('可领取7元')
				jQuery.post(url,a,function(d){
					if(d.status == 0){
						that.index.packet_five_status = 2
					}
					that.toastShow(d.msg)
				})
			
			}else if(that.index.packet_one_status == 2 || that.index.packet_two_status == 2 || that.index.packet_five_status == 2){
				console.log("已完成")
			}else{
				console.log('待领取')
				that.daiTrue = true;
				that.daiQ = i;
				if(i == 7){
					that.daiR = 5;
				}else{
					that.daiR = i;
				}
			}
		},
		hongbao(){
			if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {

				window.webkit.messageHandlers.tochangemoney.postMessage('');

			} else if (navigator.userAgent.match(/android/i)) {

				window.AndroidNative.tochangemoney();

			}
		},
		sharewxfriend(){
			var img = this.yaoqingImg;
			if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {

				window.webkit.messageHandlers.sharewxfriend.postMessage(img);

			} else if (navigator.userAgent.match(/android/i)) {

				window.AndroidNative.sharewxfriend(img);

			}
		},
		sharecirclefriend(){
			var img = this.yaoqingImg;
			if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {

				window.webkit.messageHandlers.sharecirclefriend.postMessage(img);

			} else if (navigator.userAgent.match(/android/i)) {

				window.AndroidNative.sharecirclefriend(img);

			}
		},
		buwei(x)
		{
			if(x < 10)
			{
				x = "0" + x;
			}
			return x;
		},
		fresh()
        {
			let that = this;
			
            var endtime=new Date(that.end_time);//这里设置的时间为2011年，您可以修改为其它时间。
			var nowtime = new Date(that.now_time);
			
            var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);
            var nMS = endtime.getTime() - nowtime.getTime();
            
            if(leftsecond<0){leftsecond=0;}
            __d=parseInt(leftsecond/3600/24);
            __h=parseInt((leftsecond/3600)%24);
            __m=parseInt((leftsecond/60)%60);
            __s=parseInt(leftsecond%60);
            __q=Math.floor(nMS / 100) % 10;
            
			that.tian[0].num = that.buwei(__d).toString().substr(0,1)
			that.tian[1].num  = that.buwei(__d).toString().substr(1,1)
			that.shi[0].num  = that.buwei(__h).toString().substr(0,1)
			that.shi[1].num  = that.buwei(__h).toString().substr(1,1)
			that.fen[0].num  = that.buwei(__m).toString().substr(0,1)
			that.fen[1].num  = that.buwei(__m).toString().substr(1,1)
			
            if(nMS <= 0){
                hd_end = true;
            }
        },
		toastShow(e){
			let that = this;
			that.toastMsg = e;
			that.toast = true;
			setTimeout(function()
			{
				that.toast = false;
			},1800);
		},
		qingqiu(d,platform,token){
			//接收的d是[{'a':'1'}]
			var timestamp = new Date().getTime();
			//var platform = p;
			var version = '1.0';
			var nonce = Math.floor(Math.random() * 26 + 650197);
			//var token= t;
			var device_id = "";
	
			var result ="device_id="+device_id +"&timestamp="+timestamp+"&"+"platform="+platform+"&"+"version="+version+"&"+"nonce="+nonce+"&"+"token="+token;
			var data = "{'device_id':device_id,'timestamp':timestamp,'platform':platform,'version':version,'nonce':nonce,'token':token"
			
			var canshu = d;
			
			for(var i=0;i<canshu.length;i++){
			   
				var ming = JSON.stringify(canshu[i]);
				
				ming = ming.split('"')
				
				result +="&"+ming[1]+"="+ming[3]
				
				data = data + ",'"+ming[1]+"':'"+ming[3]+"'"
			}
		   
			result = result.split("&").sort().join("&")+"HUATANG_API_SECRET"
			var sign =hex_md5(result)
			data = data + ",'sign':'"+sign+"'}"
			
			data = eval('(' + data + ')');
		   
			return data
			
		}
	}	
	
})



function GetRequest() {
	var url = location.search; //获取url中"?"符后的字串
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
		}
	}
	return theRequest;
};       
